public class test03 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int rob(TreeNode root) {
            //0 不偷 1 偷
            int[] dp = new int[2];
            dp = robMax(root);
            return Math.max(dp[0],dp[1]);
        }
        private int[] robMax(TreeNode root){
            int[] res = new int[2];
            if(root==null){
                return res;
            }

            int[] left = robMax(root.left);
            int[] right = robMax(root.right);

            res[0] = Math.max(left[0],left[1])+Math.max(right[0],right[1]);
            res[1] = left[0]+right[0]+root.val;

            return res;
        }
    }
}
